package com.spittr.web;

import com.spittr.db.ManagerRepository;
import com.spittr.db.SpitterRepository;
import com.spittr.db.SpittleRepository;
import com.spittr.domain.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * ManagerController
 *
 * @author 龙虹宇
 * @version 1.0
 * @date 2023/11/18
 */

@Controller
@RequestMapping("/m")
@SessionAttributes({ "currentPage" })
public class ManagerController {
    @Autowired // 自动注入资源
    private ManagerRepository managerRepository;

    /**
     * 分页查询管理员
     *
     * @param currentPage
     * @param model
     * @return
     */
    @RequestMapping(value = "/mList",method = GET)
    public String getAllManagersByPages(@RequestParam(value = "currentPage", defaultValue = "1")int currentPage, Model model) {
        // 上一页：判断page是否小于1
        boolean noLess = currentPage <= 1;
        if (noLess) currentPage = 1;
        List<Manager> managerList = managerRepository.selectByPage((currentPage - 1) * 5, 5);
        long size = managerRepository.count();
        // 下一页：判断page是否超限
        boolean noMore = (size - currentPage * 5L) <= 0;
        model.addAttribute("managerList", managerList);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("noLess", noLess);
        model.addAttribute("noMore", noMore);
        return "mList";
    }

    /**
     * 通过 id删除管理员
     *
     * @param managerId
     * @return
     */
    @RequestMapping(value = "/deleteManager", method = POST)
    public String deleteManagerById(@RequestParam(value = "managerId")int managerId) {
        managerRepository.deleteById(managerId);
        return "redirect:/m/mList";
    }

    /**
     * 跳转添加管理员视图
     *
     * @return
     */
    @RequestMapping(value = "/addManager", method = GET)
    public String addManager() {
        return "addManagerForm";
    }

    /**
     * 处理添加管理员表单
     *
     * @param managerForm
     * @param model
     * @return
     */
    @RequestMapping(value = "/addManager", method = POST)
    public String addManager(@ModelAttribute("managerForm")ManagerForm managerForm, Model model) {
        // 判断用户名是否存在
        if (managerRepository.findByUserName(managerForm.getUsername()) != null) {
            model.addAttribute("errorInfo", "用户名已存在，请重试");
            return "addManagerError";
        }
        System.out.println(managerForm);
        Manager manager = new Manager(managerForm.getUsername(), managerForm.getPassword(),
                managerForm.getFullName(), managerForm.getEmail(), managerForm.getPhoneNo() ,0);
        managerRepository.save(manager);
        return "redirect:/mHome";
    }

    /**
     * 跳转管理员信息页
     *
     * @return
     */
    @RequestMapping(value = "/mInfo", method = GET)
    public String getManagerInfo() {
        return "/managerInfo";
    }

    /**
     * 跳转管理员信息更新页
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/mUpdateInfo", method = GET)
    public String updateManagerInfo(Model model) {
        model.addAttribute("managerForm", new ManagerForm());
        return "/updateManagerInfo";
    }

    /**
     * 处理管理员信息更新表单
     *
     * @param managerForm
     * @param session
     * @return
     */
    @RequestMapping(value = "/mUpdateInfo", method = POST)
    public String updateManagerInfo(@ModelAttribute("managerForm")ManagerForm managerForm, HttpSession session) {
        Manager manager = new Manager();
        // 判断用户名是否冲突
        if (managerRepository.findByUserNameExceptCertainId(managerForm.getUsername(),
                ((Manager)(session.getAttribute("manager"))).getId()) != null) {
            return "/updateManagerError";
        }
        manager.setId(((Manager)(session.getAttribute("manager"))).getId());
        manager.setUsername(managerForm.getUsername());
        manager.setPassword(managerForm.getPassword());
        manager.setFullName(managerForm.getFullName());
        manager.setEmail(managerForm.getEmail());
        manager.setPhoneNo(managerForm.getPhoneNo());
        manager.setDelete(((Manager)(session.getAttribute("manager"))).getDelete());
        session.setAttribute("manager", manager);
        // 修改数据库信息
        managerRepository.updateById(((Manager)(session.getAttribute("manager"))).getId(), manager);
        return "/managerInfo";
    }
}
